package com.quiz.quazz.server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class MultipleSocketServer implements Runnable {

	private Socket connection;

	private OutputStreamWriter outputStreamWriter;

	public MultipleSocketServer(Socket s, int i) {
		this.connection = s;
	}

	public void run() {
		try {
			BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
			InputStreamReader isr = new InputStreamReader(is);
			int character;
			StringBuffer process = new StringBuffer();
			while ((character = isr.read()) != 13) {
				process.append((char) character);
			}
			interpretMessage(process.toString());

			String returnCode = "" + (char) 13;
			BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
			outputStreamWriter = new OutputStreamWriter(os, "US-ASCII");
			outputStreamWriter.write(returnCode);
			outputStreamWriter.flush();
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			try {
				connection.close();
			} catch (IOException e) {
			}
		}
	}

	/**
	 * Interprets given command and handles it
	 * 
	 * @param message
	 */
	public void interpretMessage(String message) {
		System.err.println("______" + message);
	}

	public void sendMessage(Object message) {

	}
}